package cn.eeepay.framework.dao.workOrder;

import cn.eeepay.framework.model.AgentInfo;
import cn.eeepay.framework.model.workOrder.WorkWarningInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

public interface WorkWarningWriteDao {


    @Select(
            "select * from work_warning_record where agent_no=#{agentNo} and id=#{id} "
    )
    WorkWarningInfo checkWorkWarning(@Param("id") Long id, @Param("agentNo") String agentNo);

    @Update(
            "update work_warning_record set pop_status=1 where id=#{id} "
    )
    int closeWorkWarning(@Param("id") Long id);


    class SqlProvider{

        public String selectWorkWarningList(Map<String,Object> param){
            final WorkWarningInfo info=(WorkWarningInfo)param.get("info");
            final AgentInfo loginAgent=(AgentInfo)param.get("loginAgent");
            return new SQL(){{
                SELECT("wwr.*,ai.agent_name");
                FROM("work_warning_record wwr");
                INNER_JOIN("agent_info ai on ai.agent_no=wwr.agent_no");
                if(info.getId()!=null){
                    WHERE("wwr.id=#{info.id}");
                }
                if(info.getCreateTimeBegin()!=null){
                    WHERE("wwr.create_time >= #{info.createTimeBegin}");
                }
                if(info.getCreateTimeEnd()!=null){
                    WHERE("wwr.create_time <= #{info.createTimeEnd}");
                }
                if(1==loginAgent.getAgentLevel().intValue()){//一级代理商才有的条件
                    WHERE("ai.agent_node like concat(#{loginAgent.agentNode}, '%')");
                    if(StringUtils.isNotBlank(info.getAgentNo())){
                        if(StringUtils.isNotBlank(info.getContain())){
                            if("1".equals(info.getContain())){//包含
                                WHERE("ai.agent_node like concat((select agent_node from agent_info where agent_no=#{info.agentNo}), '%')");
                            }else if("2".equals(info.getContain())){//不包含
                                WHERE("wwr.agent_no=#{info.agentNo}");
                            }else if("3".equals(info.getContain())){//仅直属下级
                                WHERE("ai.parent_id=#{info.agentNo}");
                            }else{
                                WHERE("ai.agent_node like concat((select agent_node from agent_info where agent_no=#{info.agentNo}), '%')");
                            }
                        }else{
                            WHERE("ai.agent_node like concat((select agent_node from agent_info where agent_no=#{info.agentNo}), '%')");
                        }
                    }
                }else{
                    WHERE("wwr.agent_no=#{loginAgent.agentNo}");
                }
                ORDER_BY("wwr.create_time desc");
            }}.toString();
        }
    }
}
